define(function(require, exports){
	var pubjs	= require('pubjs');
	var view  = require('@base/view');
	var popwin	= require('@base/dialog').base;
	var grid    = require('@base/grid').baseNoDate;
	var $ = require('jquery');
	var labels = require('@sys_labels').labels;
	var util = require('util');
	var Base = require('@base/common/base');

	// 表格
	var Grid = grid.extend({
		init: function(config){
			config = pubjs.conf(config, {
				cols: [

				]
				,param: {
				}
			});

			this.Super('init', arguments);
		},
		onChangeSelect: function(ev){
			var data = ev.param;
			this.fire('gridChangeSelect', data);
		}
	});
	exports.grid = Grid;

	// 主体
	var Main = view.container.extend({
		init:function(config){

			// 表格默认配置
			this.$grid_default_config = {
				sort: 'CreateTime'
				,reqType: 'websocket'
				,opClick: true
				,hasRefresh: true
				,hasTab: false
				,hasMenu: false
				,hasPager: true
				,hasSearch: true
				,hasExport: false
				,hasAmount: false
				,auto_load: true
				,hasSelect: true
				,eventDataLoad: true
				,isCheckDOM: false
				,param: {
					limit: 8
				}
				,pager: {
					size: 8
					,showSizeTypes: 0
					,showJumper: 0
				}
				,list:{
					rowSelect: true,
					scroll_type: 'row',
					scroll_size: 8
				}
			};

			config = pubjs.conf(config, {
				'url': 'deal/list',
				'param': null,
				'gridKey': '_id',
				'tab': {
					'all': {
						'text': LANG('全部'),
						'uri': 'modules/selectOnTab.grid',
						'config': {}
					},
					'select': {
						'text': LANG('已选'),
						'uri': 'modules/selectOnTab.grid',
						'config': {}
					}
				}
			});

			this.$gridData = [];
			this.$selected = [];

			this.Super('init', arguments);
		},
		afterBuild: function(){
			var self = this;
			var c = self.getConfig();
			var all = c.tab.all;
			var select = c.tab.select;

			self.create('tab', Base.tab, {
				'class': 'cool',
				'list': {
					'all': {
						'text': all.text,
						'module': {
							'uri': all.uri,
							'config': util.extend(util.clone(self.$grid_default_config), {
								param: c['param'],
								url: c['url'],
								auto_load: false,
								isCheckDOM: false
							}, all.config)
						}
					},
					'select': {
						'text': select.text,
						'module': {
							'uri': select.uri,
							'config': util.extend(util.clone(self.$grid_default_config), {
								param: c['param'],
								url: c['url'],
								auto_load: false,
								hasPager: false,
								hasSelect: false,
								isCheckDOM: false,
								'operation': {
									'render': 'renderOperation',
									'cls': 'M-gridOPCursor',
									'width': 80
								}
							}, select.config)
						}
					}
				}
			});
		},
		// 渲染操作列
		renderOperation: function(i, val, data, con){
			var html = '';

			html = [
				'<a data-op="remove" data-id="'+data.id+'">'+LANG("删除")+'</a>'
				// '<a data-op="edit" data-id="'+data.id+'">'+LANG("编辑")+'</a>',
				// '<a data-op="copy" data-id="'+data.id+'">'+LANG("复制")+'</a>'
			];

			return html.join(' | ');
		}
		,onListOpClick: function(ev){
			var c = this.getConfig();
			switch (ev.param.op){
				case 'remove':
					this.removeGridItem(ev.param.data[c.gridKey]);
					break;
			}
		}
		,removeGridItem: function(id){
			var self = this;
			var c = self.getConfig();
			util.remove(self.$selected, id);
			util.remove(self.$gridData, id, c.gridKey);
			self.setData();

			return this;
		}
		,setParam: function(param) {
			this.get('tab/all').setParam(param, true);
			return this;
		}
		,load: function(){
			this.get('tab/all').load();

			return this;
		}
		,setData: function(ids){
			var self = this;

			if(ids && ids.length){
				self.$selected = ids;

				self.get('tab/select').setParam({
					'ids': ids
				}, true).load();
			}

			self.updateTabTitle(self.$selected.length, 'select');
			self.$.tab.setData(self.$gridData, 'select');
			self.get('tab/all').setSelectRowIds(self.$selected);

			return self;
		}
		,getData: function(getAllData){
			if(getAllData){
				return this.$gridData;
			}
			return this.$selected;
		}
		,onGridDataLoad: function(ev){
			var self = this;
			if(ev){
				var name = ev.name;
				if(name == 'all'){
					// 更新标题
					self.updateTabTitle(ev.param.total, name);
				}else if(name == 'select'){
					self.$gridData = ev.param.items;
				}

			}
		}
		// 接受点选广播
		,onChangeSelect: function(ev){
			var self = this;
			var c = self.getConfig();

			// if(ev.name == 'all'){

				// 记录数据
				var data = ev.param.data;
				var selected = ev.param.selected;

				// 更新已选ids
				self.$selected = selected || [];

				// 更新表格数据
				self.$gridData = self.$gridData.concat(data);
				var result = [];
				util.each(self.$selected, function(sel){
					if(sel){
						var item = util.find(self.$gridData, sel, c.gridKey);
						if(item){
							result.push(item);
						}
					}
				});
				self.$gridData = result;

				// 更新数据
				this.setData();
			// }
		}
		// 更新tab某项的标题
		,updateTabTitle: function(length, tabName){
			var self = this;
			var c = self.getConfig();
			if(self.get('tab')){
				var title = c.tab[tabName].text + ' ('+ (length||0) +')';
				self.get('tab').updateTitle(title, tabName);
			}
			return self;
		}
		,reset: function(){
			this.$gridData = [];
			this.$selected = [];
			this.setData();

			return this;
		}
	});
	exports.main = Main;

	// 弹层
	var Pop = popwin.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'title': LANG(''),
				'width': 800,
				'showFoot': true,
				'param': null,
				'class': '',
				'silence': false,
				'size': 'm',
				'height': 650,
				'width': 1000,
				'selectEvent': '',
				'getAllData': false, // 是否获取具体数据，默认获取id数组
				'main_config': {

				}
				// 'buttons': ['cancel', null]
			});


			this.Super('init', arguments);
		},
		afterBuild:function() {
			this.Super('afterBuild', arguments);
			var c = this.getConfig();

			this.create('main', Main, util.extend({}, c.main_config));
		}
		,setParam: function(param) {
			this.get('main').setParam(param);
			return this;
		}
		,show: function() {
			this.setWidth(this.getConfig('width'));
			this.Super('show', arguments);

			return this;
		}
		,load: function(){
			this.get('main').load();
			return this;
		}
		,setData: function(data){
			var self = this;
			self.get('main').setData(data);
			return self;
		}
		,getData: function(){
			var getAllData = this.getConfig('getAllData');
			return this.get('main').getData(getAllData);
		}
		,onOk: function(ev){
			var self = this;
			var c = self.getConfig();
			self.fire(c.selectEvent, self.getData());
			self.hide();
		}
		,reset: function(){
			this.get('main').reset();
			return this;
		}
	});
	exports.pop = Pop;

});